package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.fragments.CardsAndCouponsFragment;
import com.sphd.Restaurant.fragments.HomeFragment;
import com.sphd.Restaurant.fragments.OrderFragment;
import com.sphd.Restaurant.fragments.SettingFragment;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.checkup.CheckUpdate;

public class MainActivity extends BaseActivity {

    @BindView(R.id.my_fragment)
    FrameLayout myFragment;
    @BindView(R.id.btn_allres)
    ImageButton btnAllres;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.home_layout)
    LinearLayout homeLayout;
    @BindView(R.id.btn_cars_coupons)
    ImageButton btnCarsCoupons;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.cards_layout)
    LinearLayout cardsLayout;
    @BindView(R.id.btn_order)
    ImageButton btnOrder;
    @BindView(R.id.tv_dingdan)
    TextView tvDingdan;
    @BindView(R.id.order_layout)
    LinearLayout orderLayout;
    @BindView(R.id.btn_setting)
    ImageButton btnSetting;
    @BindView(R.id.tv_set)
    TextView tvSet;
    @BindView(R.id.set_layout)
    LinearLayout setLayout;
    HomeFragment homeFragment;
    CardsAndCouponsFragment couponsFragment;
    OrderFragment orderFragment;
    SettingFragment settingFragment;
    FragmentManager fm;
    private static final String PRV_SELINDEX = "PREV_SELINDEX";
    private int selindex = 0;
    Intent intent;
    Bundle bundle;
    /**
     * Fragment的TAG 用于解决app内存被回收之后导致的fragment重叠问题
     */
    private static final String[] FRAGMENT_TAG = {"0", "1", "2", "3"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        intent = getIntent();
        bundle = intent.getExtras();
        initView(savedInstanceState);
        CheckUpdate checkUpdate = new CheckUpdate(context);
        checkUpdate.checkUpAuto(Model.BASEURL+Model.UPDATA);
    }
    private void initView(Bundle savedInstanceState) {
        fm = getSupportFragmentManager();
        if (savedInstanceState != null) {
            //读取上一次界面Save的时候tab选中的状态
            selindex = savedInstanceState.getInt(PRV_SELINDEX, selindex);
            homeFragment = (HomeFragment) fm.findFragmentByTag(FRAGMENT_TAG[0]);
            couponsFragment = (CardsAndCouponsFragment) fm.findFragmentByTag(FRAGMENT_TAG[1]);
            orderFragment = (OrderFragment) fm.findFragmentByTag(FRAGMENT_TAG[2]);
            settingFragment = (SettingFragment) fm.findFragmentByTag(FRAGMENT_TAG[3]);
        }
            if (bundle.getInt(Constants.JUMPPAGE, 0) != 0) {
                AppManager.getAppManager().delAllActivityExceptMain(MainActivity.class);
                selindex = bundle.getInt(Constants.JUMPPAGE);
            }

        select(selindex);
    }

    private void select(int i) {
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragemt(transaction);
        switch (i) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.my_fragment, homeFragment, FRAGMENT_TAG[0]);
                } else {
                    transaction.show(homeFragment);
                }
                btnAllres.setBackgroundResource(R.mipmap.shouye_down);
                tvHome.setTextColor(getResources().getColor(R.color.mycolor));
                break;
            case 1:
                if (couponsFragment == null) {
                    couponsFragment = new CardsAndCouponsFragment();
                    transaction.add(R.id.my_fragment, couponsFragment, FRAGMENT_TAG[1]);
                } else {
                    transaction.show(couponsFragment);
                }
                btnCarsCoupons.setBackgroundResource(R.mipmap.kaquan_down);
                tvCard.setTextColor(getResources().getColor(R.color.mycolor));
                break;
            case 2:
                if (orderFragment == null) {
                    orderFragment = new OrderFragment();
                    transaction.add(R.id.my_fragment, orderFragment, FRAGMENT_TAG[2]);
                } else {
                    transaction.show(orderFragment);
                }
                btnOrder.setBackgroundResource(R.mipmap.dingdan_down);
                tvDingdan.setTextColor(getResources().getColor(R.color.mycolor));
                break;
            case 3:
                if (settingFragment == null) {
                    settingFragment = new SettingFragment();
                    transaction.add(R.id.my_fragment, settingFragment, FRAGMENT_TAG[3]);
                } else {
                    transaction.show(settingFragment);
                }
                btnSetting.setBackgroundResource(R.mipmap.more_down);
                tvSet.setTextColor(getResources().getColor(R.color.mycolor));
                break;
        }
        transaction.commit();
    }

    /**
     * @param transaction 隐藏掉所有的fragment
     */
    private void hideFragemt(FragmentTransaction transaction) {
        hideFragemts(transaction, homeFragment);
        hideFragemts(transaction, couponsFragment);
        hideFragemts(transaction, orderFragment);
        hideFragemts(transaction, settingFragment);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //保存tab选中的状态
        outState.putInt(PRV_SELINDEX, selindex);
        super.onSaveInstanceState(outState);
    }


    @OnClick({R.id.btn_allres, R.id.btn_cars_coupons, R.id.btn_order, R.id.btn_setting})
    public void onClick(View view) {
        hideImage();
        switch (view.getId()) {
            case R.id.btn_allres:
                select(0);
                break;
            case R.id.btn_cars_coupons:
                select(1);
                break;
            case R.id.btn_order:
                select(2);
                break;
            case R.id.btn_setting:
                select(3);
                break;
        }
    }

    private void hideImage() {
        btnAllres.setBackgroundResource(R.mipmap.shouye);
        tvHome.setTextColor(getResources().getColor(R.color.main_botoom_tab_text));
        btnCarsCoupons.setBackgroundResource(R.mipmap.kaquan);
        tvCard.setTextColor(getResources().getColor(R.color.main_botoom_tab_text));
        btnOrder.setBackgroundResource(R.mipmap.dingdan);
        tvDingdan.setTextColor(getResources().getColor(R.color.main_botoom_tab_text));
        btnSetting.setBackgroundResource(R.mipmap.more);
        tvSet.setTextColor(getResources().getColor(R.color.main_botoom_tab_text));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }
}
